package com.lbb.springboot.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DataModel {
	private int status=0;//1 正常    其他状态为服务器异常
	private String returntype;//0   提示性文本 1 JSONObject 2 JSONArray
	private int key;
	private String message;
	private PageModel pagemodel;
	private String pagehtml;
	private Object list;
	private Object data;
	private String totalPrice;
	private String aount;
	private String yearAount;


	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public DataModel(){
		pagemodel=new PageModel();
	}
	
	public void setPagehtml(){
		pagehtml=pagemodel.getHtml();
	}
	
	public DataModel setUrl(String url){
		pagemodel.setUrl(url);
		this.setPagehtml();
		return this;
	}
	
	public void setPage(int totalRecords, int pageSize, int pageNo){
		pagemodel.setPage(totalRecords, pageSize, pageNo);
	}
	
	public String getPagehtml() {
		return pagehtml;
	}

	public void setPagehtml(String pagehtml) {
		this.pagehtml = pagehtml;
	}
	public PageModel getPagemodel() {
		return pagemodel;
	}

	public void setPagemodel(PageModel pagemodel) {
		this.pagemodel = pagemodel;
	}

	public Object getList() {
		return list;
	}

	public void setList(Object list) {
		this.key=DataModel.ResponseCode.SUCCESS;
		this.list = list;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getReturntype() {
		return returntype;
	}

	public void setReturntype(String returntype) {
		this.returntype = returntype;
	}

	public int getKey() {
		return key;
	}

	public void setKey(int key) {
		this.key = key;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 向浏览器输出返回对象的内容	/**
	 * 返回数据转json
	 */
	public String toJsonString(){
		  pagemodel=null;
		  String str= JSON.toJSONString(this);
		 // LOG.out("web back:"+str);
		  return str;
	}


	public void outFail(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.FAIL;
		this.message="失败";
		out(response);

	}
	public void outxun(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.FAIL;
		this.message="该序号已经存在！";
		out(response);

	}
	public void outGuan(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.FAIL;
		this.message="该投票已经关联或序号已存在！";
		out(response);

	}

	/**
	 * 成功返回
	 * @param response
	 */
	public void outSuccess(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.SUCCESS;
		this.message="成功";
		out(response);

	}


	/**
	 * 向浏览器输出返回对象的内容(状态，状态说明)
	 *
	 */
	public void out(HttpServletResponse response, int key, String message){
		this.status=DataModel.ServerStatus.OK;
		this.key=key;
		this.message=message;
		out(response);
	}


	@Override
	public String toString() {
		return "DataModel [status=" + status + ", returntype=" + returntype
				+ ", key=" + key + ", message=" + message + ", pagemodel="
				+ pagemodel + ", pagehtml=" + pagehtml + ", list=" + list + "]";
	}

	public void out(HttpServletResponse response){
		PrintWriter out;
		try {
			out = response.getWriter();
			out.write(this.toJsonString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



	/**
	 * 必要字段为空时返回
	 */
	public void outEmptyParams(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.NECESSARYPARAMSEMPTY;
		this.message="必要参数为空";
		out(response);
	}

	/**
	 * 数据为空时返回
	 */
	public void outEmptyData(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.NOEMPTY;
		this.message="没有相关数据";
		out(response);
	}


	public void  abnormal(HttpServletResponse response){
		this.status=DataModel.ServerStatus.OK;
		this.key=DataModel.ResponseCode.SERVERABNORMAL;
		this.message="服务器异常";
		out(response);
	}



	/**
	 * 服务器异常返回
	 */
	public void abnormal(HttpServletResponse response, int key, String message){
		this.status=DataModel.ServerStatus.OK;
		this.key=key; 
		this.message=message;
		out(response);
	}

	public String getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(String totalPrice) {
		this.totalPrice = totalPrice;
	}

	public String getAount() {
		return aount;
	}

	public void setAount(String aount) {
		this.aount = aount;
	}

	public String getYearAount() {
		return yearAount;
	}

	public void setYearAount(String yearAount) {
		this.yearAount = yearAount;
	}


	/**
	 * 服务器异常
	 */
/*	public void abnormal(HttpServletResponse response,RequestParams params){
		this.status=DataModel.ServerStatus.OK;
		this.key=params.getStatus();
		this.message=params.getDesc();
		out(response);
	}
	*/
	
	

	/**
	 * 返回类型
	 */
		public class ResponseType{
			/**
			 * 文本
			 */
			public static final int TEXT=0;
			/**
			 * json对象
			 */
			public static final int JSONOBJECT=1;
			/**
			 * json数组
			 */
			public static final int JSONARRAY=1;
		}
		
		
		/**
		 * 返回状态
		 */
		public class ResponseCode{
			/**
			 * 失败
			 */
			public static final int FAIL=0;
			/**
			 * 成功
			 */
			public static final int SUCCESS=1;
			
			/**
			 * 服务器异常
			 */
			public static final int SERVERABNORMAL=2;
			
			/**
			 * 没有相关数据
			 */
			public static final int NOEMPTY=3;
			
			/**
			 * 必要参数为空
			 */
			public static final int NECESSARYPARAMSEMPTY=4;
			
			
		}
		
		public class ServerStatus{
			/**
			 * 服务器异常
			 */
			public static final int ERROR=0;
			/**
			 * 服务器正常
			 */
			public static final int OK=1;
		}
		
	
	
	
	
	
	
}
